Public Enum TimeMode
   Second
   Minute
   Hour
End Enum

Public Class DateTimeUpDown

   Private CopyrightName As String = "Ali Karbassi"
   Private theTime As Date
   Private prevValue As Integer

   Private _caption As String = "Caption"
   Private _mode As TimeMode = TimeMode.Minute
   Private _unit As Short = 1
   Private _unitDefault As Short = 1

   Public Property Caption() As String
      Get
         Return _caption
      End Get
      Set(ByVal value As String)
         _caption = value
         lblCaption.Text = value
         lblCaption.Left = (Me.ClientSize.Width / 2) - (lblCaption.Width / 2)
      End Set
   End Property

   Public Property Mode() As TimeMode
      Get
         Return _mode
      End Get
      Set(ByVal value As TimeMode)
         _mode = value
         Unit = _unitDefault
      End Set
   End Property

   Public Property Unit() As Short
      Get
         Return _unit
      End Get
      Set(ByVal value As Short)

         If Mode = TimeMode.Hour Then
            If value < 1 Or value > 24 Then
               Throw New Exception("Unit value is out of range.")
            Else
               _unit = value
            End If
         Else
            If value < 1 Or value > 60 Then
               Throw New Exception("Unit value is out of range.")
            Else
               _unit = value
            End If
         End If

      End Set
   End Property


   Private Sub DateTimeUpDown_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      prevValue = NumericUpDown1.Value

      lblCopyRight.Text = "Copyright " & Convert.ToChar(169) & " 2007 by " & CopyrightName
      lblCopyRight.Left = (Me.ClientSize.Width / 2) - (lblCopyRight.Width / 2)

      theTime = Now
      txtDateTime.Text = theTime

   End Sub

   Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged

      ' Did it move up or down?
      Dim up As Short
      If NumericUpDown1.Value - prevValue > 0 Then
         up = 1
         NumericUpDown1.Maximum += _unit
      Else
         up = -1
         NumericUpDown1.Minimum -= _unit
      End If
      prevValue = NumericUpDown1.Value
      ' Done


      If Mode = TimeMode.Hour Then
         theTime = DateAdd(DateInterval.Hour, up * _unit, theTime)
      ElseIf Mode = TimeMode.Minute Then
         theTime = DateAdd(DateInterval.Minute, up * _unit, theTime)
      ElseIf Mode = TimeMode.Second Then
         theTime = DateAdd(DateInterval.Second, up * _unit, theTime)
      End If

      txtDateTime.Text = theTime

   End Sub
End Class
